package com.tanhua.sso.controller;

import com.tanhua.sso.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("users")
public class UsersController {

    @Autowired
    private UserInfoController userInfoController;
    @Autowired
    private UsersService usersService;

    /**
     * 上传头像
     */
    @RequestMapping("header")
    @PostMapping
    public ResponseEntity<Object> saveLogo(@RequestParam("headPhoto") MultipartFile headPhoto,
                                           @RequestHeader("Authorization") String token) throws IOException {
        return this.userInfoController.saveLogo(headPhoto, token);
    }

    /**
     * 发送验证码
     */
    @PostMapping("phone/sendVerificationCode")
    public ResponseEntity<Void> sendVerificationCode(@RequestHeader("Authorization") String token) throws IOException {
        this.usersService.sendVerificationCode(token);
        return ResponseEntity.ok(null);
    }


    /**
     * 校验验证码
     */
    @PostMapping("phone/checkVerificationCode")
    public ResponseEntity<Map<String, Object>> checkVerificationCode(@RequestBody Map<String, String> param,
                                                                     @RequestHeader("Authorization") String token) throws IOException {
        String code = param.get("verificationCode");
        Boolean bool = this.usersService.checkVerificationCode(code, token);
        Map<String, Object> result = new HashMap<>();
        result.put("verification", bool);
        return ResponseEntity.ok(result);
    }

    /**
     * 保存新的手机号
     */
    @PostMapping("phone")
    public ResponseEntity<Void> saveNewMobile(@RequestBody Map<String, String> param,
                                              @RequestHeader("Authorization") String token) throws IOException {
        String newPhone = param.get("phone");
        this.usersService.updateNewMobile(token, newPhone);
        return ResponseEntity.ok(null);
    }
}
